import json
import urllib.parse as u_parse


def replace_card(input:str,file_name=r'test.txt'):
    # extract variableName=valuable
    print(file_name)
    var_val=dict_keyIsVar_Val(input)
    payload = create_payload(var_val,file_name)
    print(u_parse.unquote(payload))
    print('\npayload:\n')
    print(payload)
    return payload


def dict_keyIsVar_Val(input:str,split_sign='&'):
    '''
    input:aid=404066758&cid=1215679895&type=2&cards=%5B%7B%22from%22%3A0%2C%22to%22%3A10%2C%22content%22%3A%22test%22%7D%2C%7B%22from%22%3A10%2C%22to%22%3A247%2C%22content%22%3A%22test2%22%7D%5D&permanent=true&csrf=94bf22466ece8dd24447c9eece2a2aaf
    return:[aid=...,cid=...,...]
    :param input:
    :param split_sign:
    :return:
    '''
    result_dict={}
    input = u_parse.unquote(input)
    has_eq=input.split(split_sign)#
    for i in has_eq:
        # print(i)
        key_val=i.split('=')

        key = key_val[0]
        val = key_val[1]
        result_dict[key]=val
    return result_dict

def create_payload(var:dict,file_name):
    '''
    生成cards需要的时间序列（分章节）
    :param var:
    :return:
    '''

    cards = time2ft(file_name)
    var['cards'] = cards

    result_str = ""
    for idx, (k, v) in enumerate(var.items()):
        tmp_str=k +'='+u_parse.quote(v)
    # 判断是否为最后一个元素
        if idx < len(var) - 1:
            tmp_str+='&'

        result_str+=tmp_str

    return result_str





def read_file(filename):
    # 读取文件内容并返回一个包含每一行内容的列表
    with open(filename, 'r', encoding='utf-8') as file:
        return file.readlines()

def time2ft(file_path):

    '''
00:00:00 : A Head Full Of Dreams
00:05:38 : Yellow
00:10:33 : Every Teardrop Is A Waterfall

to

[{"from":开始,"to":结束秒数,"content":"备注"},{"from":开始,"to":结束秒数,"content":"备注"}]
    :param timelist:
    :return:
    '''
    ftlist=[]
    tmp_time=0
    timelines = read_file(file_path)
    for line in range(len(timelines)):
        # print('line',line)
        line0 = timelines[line].strip()
        if line != len(timelines)-1:
            line1 = timelines[line+1].strip()
            time1 = line1[:8]
            time_sec = time_to_seconds(time1)#from
        else:
            time_sec =tmp_time+1

        content0 = line0[8:].strip().strip(':').strip()



        dic = {'from':tmp_time,'to':time_sec ,'content':content0}
        tmp_time=time_sec

        ftlist.append(dic)


    # print('ftlist',json.dumps(ftlist,separators=(',',':')))
    return json.dumps(ftlist,separators=(',',':'))



def time_to_seconds(time_str):
    # 将时间字符串拆分成时、分、秒三个部分
    hours, minutes, seconds = time_str.split(':')

    # 将时、分、秒转换为整数
    hours = int(hours)
    minutes = int(minutes)
    seconds = int(seconds)

    # 计算总秒数
    total_seconds = hours * 3600 + minutes * 60 + seconds

    return total_seconds

def write_file(filename, content):
    # 将修改后的文件内容写入到指定文件中
    with open(filename, 'w', encoding='utf-8') as file:
        file.writelines(content)



if __name__ == '__main__':
    val = 'aid=404066758&cid=1215679895&type=2&cards=%5B%7B%22from%22%3A0%2C%22to%22%3A10%2C%22content%22%3A%22test%22%7D%2C%7B%22from%22%3A10%2C%22to%22%3A247%2C%22content%22%3A%22test2%22%7D%5D&permanent=true&csrf=94bf22466ece8dd24447c9eece2a2aaf'

    val = input('请输入载荷\n')
    file_name = r'D:\coding\python\test-code\cha\test.txt'

    output=replace_card(val,file_name)

    write_file('payload.txt',output)
